﻿using System.Collections.Generic;
using System.Windows.Forms;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Service;
using HR60_PanelSoft.Model.BaseModel;

namespace HR60_PanelSoft.View.Base
{
	/// <summary>
	/// 基本数据操作，为简化视图层操作，直接取值
	/// </summary>
	public partial class BaseData : UserControl
	{
		// 当前项目
		protected static Proj proj
		{
			get { return ProjService.getCurrentProj(); }
		}

		// 设备分组
		protected static IList<DeviceGroup> deviceGroups
		{
			get { return proj.deviceGroups; }
		}

		// 场景列表
		protected static IList<Scene> scenes
		{
			get { return proj.scenes; }
		}

		// 面板型号列表
		protected static IList<PanelType> panelTypes
		{
			get { return PanelTypeService.findList(); }
		}

		// 设备型号分组
		protected static string[] deviceTypeGroups
		{
			get { return DeviceTypeService.getCategorys(); }
		}

		// 设备型号列表
		protected static IDictionary<string, List<DeviceType>> deviceTypes
		{
			get { return DeviceTypeService.findListSortByGroup(); }
		}

		// 根据uuid查找设备
		protected static Device findDeviceByUuid(string uuid)
		{
			foreach (DeviceGroup group in deviceGroups)
			{
				foreach (Device dev in group.devices)
				{
					if (dev.uuid == uuid)
						return dev;

					if (dev.type.subDeviceType != null)
					{
						foreach (Device sub in dev.childDevices)
						{
							if (sub.uuid == uuid)
								return sub;
						}
					}
				}
			}
			return null;
		}

		// 根据uuid查找设备，如果是分机，则同时返回主设备
		protected static void findDeviceByUuid(string uuid, ref Device device, ref Device subDevice)
		{
			foreach (DeviceGroup group in deviceGroups)
			{
				foreach (Device dev in group.devices)
				{
					if (dev.uuid == uuid)
					{
						device = dev;
						return;
					}

					if (dev.type.subDeviceType != null)
					{
						foreach (Device sub in dev.childDevices)
						{
							if (sub.uuid == uuid)
							{
								device = dev;
								subDevice = sub;
                                return;
							}
						}
					}
				}
			}
		}

		// 根据uuid查找设备组
		protected static DeviceGroup findGroupByDeviceUuid(string uuid)
		{
			foreach (DeviceGroup group in deviceGroups)
			{
				foreach (Device device in group.devices)
				{
					if (device.uuid == uuid)
						return group;

					if (device.type.subDeviceType != null)
					{
						foreach (Device sub in device.childDevices)
						{
							if (sub.uuid == uuid)
								return group;
						}
					}
				}
			}
			return null;
		}

		// 根据uuid查找场景
		protected static Scene findSceneByUuid(string uuid)
		{
			foreach (Scene scene in scenes)
			{
				if (scene.uuid == uuid)
				{
					return scene;
				}
			}
			return null;
		}

		// 根据类型获得高级设备
		protected static Device findExDevice(ExDeviceType exType)
		{
			foreach (DeviceGroup group in deviceGroups)
			{
				foreach (Device device in group.devices)
				{
					if (device.type.exType == exType)
						return device;
				}
			}
			return null;
		}
	}
}
